/*
 * $$Id: PageRefTag.java,v 1.7 2004/05/24 04:50:20 alexnet Exp $$
 *
 * ***** BEGIN LICENSE BLOCK *****
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License
 * at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and
 * limitations under the License.
 *
 * The Original Code is jforum forum code.
 *
 * The Initial Developer of the Original Code is the JResearch, Org.
 * Portions created by the Initial Developer are Copyright (C) 2004
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *              Dmitry Belov <bel@jresearch.org>
 *
 * ***** END LICENSE BLOCK ***** */
/*
 * Created on Nov 9, 2003
 *
 */
package com.jred.jforum.tags;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import javax.xml.registry.infomodel.User;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;

import com.jred.jforum.beans.forum.Topic;



/**
 * DOCUMENT ME!
 *
 * @author Bel
 */
public class PageRefTag extends TagSupport {

	/**
	 * Logger instance.
	 */
	private static final Log LOG = LogFactory.getLog(PageRefTag.class);

    // name of topic bean from page scope
    private String threadBean;

    // name of forumid bean from page scope
    private String forumId;

    private void eval() throws JspException {
        forumId = ExpressionEvaluatorManager.evaluate("forumId", forumId,
                Integer.class, this, pageContext).toString();
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     *
     * @throws JspException DOCUMENT ME!
     */
    public int doStartTag() throws JspException {
        eval();

        try {
            HttpSession session = pageContext.getSession();
            HttpServletResponse response = (HttpServletResponse) pageContext.getResponse();
            //User user = (User) session.getAttribute(IConst.SESSION.USER_KEY);
            Topic t = (Topic) pageContext.getAttribute(threadBean);
            int total = (int) t.getMessagesCount();

            if (total > 10) {
                StringBuffer links = new StringBuffer("(");

                int rep = (int) Math.floor(total / 10) + 1;
                int i = 1;

                while ((i <= rep) &&
                        (((i - 1) * 10) < total)) {
                    StringBuffer href = new StringBuffer();
                    links.append("<a href=\"");
                    href.append("ShowThread.do?fid=");
                    href.append(forumId);
                    href.append("&tid=");
                    href.append(t.getThreadid());
                    href.append("&block=");
                    href.append((i - 1) * 10);
                    links.append(response.encodeURL(href.toString()));
                    links.append("\" class=\"thread_name\" >");
                    links.append(i);
                    links.append("</a>");

                    if ((++i) <= rep) {
                        links.append(", ");
                    }
                }

                links.append(")");

                JspWriter out = pageContext.getOut();
                out.print(links.toString());
            }
        } catch (IOException e) {
			if(LOG.isErrorEnabled())
			{
				LOG.error("PageRefTag::", e);
			}
            throw new JspException("error in PageRefTag tag:", e);
        }

        return (SKIP_BODY);
    }

    /**
     * DOCUMENT ME!
     *
     * @return
     */
    public String getForumId() {
        return forumId;
    }

    /**
     * DOCUMENT ME!
     *
     * @return
     */
    public String getThreadBean() {
        return threadBean;
    }

    /**
     * DOCUMENT ME!
     *
     * @param string
     */
    public void setForumId(String string) {
        forumId = string;
    }

    /**
     * DOCUMENT ME!
     *
     * @param string
     */
    public void setThreadBean(String string) {
        threadBean = string;
    }
}
